package com.swanclouds.redfish.identity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.swanclouds.model.identity.AuthStore;
import com.swanclouds.model.identity.Token;
import com.swanclouds.model.shared.RootResource;
import com.swanclouds.redfish.shared.RootResourceConcrete;

import java.util.Date;
import java.util.UUID;

public class RedfishToken implements Token {

    @JsonProperty("X-Auth-Token")
    private String id;

    @JsonProperty("ETag")
    private String eTag;

    private String endpoint;
    private Date expires;

    @JsonProperty("Location")
    private String location;

    @JsonIgnore
    private RootResourceConcrete rootResource;

    private RedfishAuth credentials;

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String getETag() {
        return this.eTag;
    }

    @Override
    public void setETag(String eTag) {
        this.eTag = eTag;
    }

    @Override
    public Date getExpires() {
        return expires;
    }

    @Override
    public String getEndpoint() {
        return this.endpoint;
    }

    @Override
    public String getLocation() {
        return this.location;
    }

    public void setExpires(Date expires) {
        this.expires = expires;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @JsonIgnore
    @Override
    public String getCacheIdentifier() {
        String uniq = UUID.randomUUID().toString();
        return String.format("%s:%s", endpoint, uniq);
    }

    @Override
    public RootResource getRootResource() {
        return this.rootResource;
    }

    public void setRootResource(RootResourceConcrete rootResource) {
        this.rootResource = rootResource;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    @Override
    public RedfishAuth getCredentials() {
        return credentials;
    }

    public void setCredentials(RedfishAuth credentials) {
        this.credentials = credentials;
    }

    public RedfishToken applyContext(String endpoint, RedfishAuth credentials) {
        this.endpoint = endpoint;
        this.credentials = credentials;
        return this;
    }

    public RedfishToken applyContext(String endpoint) {
        this.endpoint = endpoint;
        return this;
    }

    public RedfishToken applyContext(String endpoint, AuthStore credentials) {
        this.endpoint = endpoint;
        this.credentials = new RedfishAuth(credentials.getUsername(),credentials.getPassword());
        return this;
    }

    @Override
    public String toString() {
        return "RedfishToken{" +
                "id='" + id + '\'' +
                ", eTag='" + eTag + '\'' +
                ", endpoint='" + endpoint + '\'' +
                ", expires=" + expires +
                ", location='" + location + '\'' +
                ", credentials=" + credentials +
                ", rootResource=" + rootResource +
                '}';
    }
}
